﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;

namespace TIMSComponents.Choisers
{
    public partial class ListContainer : Form
    {
        private DataAccessLayer DAL = new DataAccessLayer();

        private DataTable DataSource = new DataTable();
        private string FieldKeyNN;
        private string FieldKeyNS;
        private string SortFN;
        private int[] CollumeWidth;        
        private string[] ReturnedField;
        private string[] CollumsCaption;

        internal StringDictionary Result;

        
        public ListContainer(DataTable pDataSource, string pFieldKeyNN, string pFieldKeyNS, string pSortFN, int[] pCollumeWidth,string[] pColumnCaption, string[] pReturnedField)
        {
            InitializeComponent();

            DataSource = pDataSource;
            FieldKeyNN = pFieldKeyNN;
            FieldKeyNS = pFieldKeyNS;
            SortFN = pSortFN;
            CollumeWidth = pCollumeWidth;
            ReturnedField = pReturnedField;
            CollumsCaption = pColumnCaption;
        }

        #region Just do it :)
        //[DllImport("user32", CharSet = CharSet.Auto)]
        //private extern static int SendMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);

        //[DllImport("user32", CharSet = CharSet.Auto)]
        //private extern static int PostMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);

   
        //public static int SW_SHOWNOACTIVATE = 4;
        //public static int SW_SHOWNA = 8;

        //[DllImport("user32.dll")]
        //public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        //public void ShowWithoutActivating()
        //{
        //    ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
          
            

        //}

        //private const int WS_SYSMENU = 0x80000;
        //private const int WS_POPUP = 0x8000000;

        //private const int WS_EX_TOOLWINDOW = 0x00000080;
        //private const int WS_EX_NOACTIVATE = 0x08000000;

        //public new void Show(int i)
        //{
        //    if (this.Handle == IntPtr.Zero) base.CreateControl();

        //    SetParent(base.Handle, IntPtr.Zero);
        //    ShowWindow(base.Handle, 1);
         
        //}

        //[DllImport("user32")]
        //public static extern int SetParent
        // (IntPtr hWndChild, IntPtr hWndNewParent);

        //[DllImport("user32")]

        //public static extern int ShowWindow
        // (IntPtr hWnd, int nCmdShow);

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.Style |= WS_POPUP;
        //        cp.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
        //        //cp.Parent = IntPtr.Zero; // Keep this line only if you used UserControl
        //        return cp;
        //    }
        //}

        #endregion


        private void Sort()
        {
            if (string.IsNullOrEmpty(SortFN) == false && gridView1.Columns.Count > 0)
            {
                bindingSource1.Sort = SortFN + " asc"; 
            }
        }

        public void SetCollumeName()
        {
            //gridView1.Columns[i].Caption=""
            int MinItem;
            if (CollumsCaption != null)
            {
                //Get minimum size array 
                MinItem = Math.Min(CollumsCaption.Length, gridView1.Columns.Count);
                for (int i = 0; i < MinItem; i++)
                {
                    gridView1.Columns[i].Caption = CollumsCaption[i].ToString();
                }
            }
  
        }

        public void SetCollumeSize()
        {
            int MinItem;
            if (CollumeWidth != null && CollumeWidth.Length!=0)
            {
                //Get minimum size array
                MinItem = Math.Min(CollumeWidth.Length, gridView1.Columns.Count);
                for (int i = 0; i < MinItem - 1; i++)
                {
                    gridView1.Columns[i].Width = CollumeWidth[i];
                }
            }
            else
            {
                for (int i = 0; i < gridView1.Columns.Count; i++)
                {
                    gridView1.Columns[i].BestFit();
                }
            }
        }

        //Filtering data
        internal void OnExpressionTextChanged(string Txt)
        {
            if (string.IsNullOrEmpty(Txt))
            {
                bindingSource1.DataSource = DataSource;
            }
            else
            {
                bindingSource1.DataSource = DAL.GetFilteredData(DataSource, CMain.GetWhereCondition(FieldKeyNN, FieldKeyNS, Txt));
            }
        }

        //Manage key down events
        public void KeyDownEvent(KeyEventArgs e)
        {
            if (gridView1.RowCount < 0)
            {
                return;
            }
            switch (e.KeyCode)
            {
                case  Keys.Down:
                    gridView1.UnselectRow(bindingSource1.Position);
                    bindingSource1.MoveNext();
                    gridView1.SelectRow(bindingSource1.Position);                    
                    break;
                case Keys.Up:
                    gridView1.UnselectRow(bindingSource1.Position);
                    bindingSource1.MovePrevious();
                    gridView1.SelectRow(bindingSource1.Position);
                    break;
                case Keys.Enter:
                    FillResult();
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
            }            
        }

        private void ListContainer_Load(object sender, EventArgs e)
        {    
            bindingSource1.DataSource=DataSource;
            gridControl1.DataSource=bindingSource1;
            Sort();
            SetCollumeName();
            SetCollumeSize();
        }

        //Manage forms size by RowHeight. max height 200
        private void gridView1_RowCountChanged(object sender, EventArgs e)
        {
            this.Height = Math.Min((gridView1.RowHeight * gridView1.RowCount) + 20+statusStrip1.Height+ 20,200);
        }

        private void FillResult()
        {
            if (ReturnedField != null)
            {
                Result = new StringDictionary();
                int MinItem = Math.Min(ReturnedField.Length, gridView1.Columns.Count);
                for (int i = 0; i < MinItem; i++)
                {
                    Result.Add(ReturnedField[i], gridView1.GetFocusedRowCellDisplayText(ReturnedField[i].ToString()));
                }                
                this.Close();
                
            }
            else
            {
                MessageBox.Show("ReturnedField's value is null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                FillResult();
            }
            
        }

        private void ListContainer_SizeChanged(object sender, EventArgs e)
        {
            
        }

        private void ListContainer_ResizeBegin(object sender, EventArgs e)
        {
            
        }



    }
}
